package android.medicalcloud.com.yiapp.base.net;

import android.medicalcloud.com.yiapp.base.exc.BaseException;
import android.medicalcloud.com.yiapp.base.net.lisener.HttpNetCallBack;
import android.medicalcloud.com.yiapp.base.net.lisener.NetCallback;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.base.utils.MapUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by wanggenlin on 17-6-2.
 */
public class OkHttpManager implements HttpNetCallBack {

    @Override
    public void get(String url, Map<String, Object> params, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {

    }

    @Override
    public void getWithHeader(String url, Map<String, Object> params, Map<String, String> header, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {

        OkHttpClient mOkHttpClient=new OkHttpClient();
        url = MapUtils.map2UrlParams(url, params);
        Request.Builder requestBuilder = new Request.Builder().url(url);
        //可以省略，默认是GET请求
        requestBuilder.method("GET",null);
        setHeader(requestBuilder,header);
        Request request = requestBuilder.build();
        Call mcall= mOkHttpClient.newCall(request);
        try {
            Response response = mcall.execute();

            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static  void setHeader(Request.Builder requestBuilder, Map <String,String> header){
        if (header==null){
            return;
        }

        Iterator iter = header.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            Logger.e("header=="+key+" "+val);

            requestBuilder.addHeader(key, val);
        }
        requestBuilder.addHeader("Content-Type", "application/json;charset=UTF-8");
    }

    @Override
    public void post(String url, Map<String, Object> params, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {

    }

    @Override
    public void postWithHeader(String url, Map<String, Object> params, Map<String, String> header, long timeOut, boolean cach, NetCallback responesCallBack) throws BaseException {




    }

    @Override
    public void cancle() {

    }
}
